package jvisualfs;

/*
 * MainUI.java
 *
 * Copyright 2006, Dag Rune Sneeggen
 * 
 * This file is part of jVisualFS.
 * Version: release-0.8_beta
 *
 * jVisualFS is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * jVisualFS is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with jVisualFS.  If not, see <http://www.gnu.org/licenses/>.
 * 
 */



//import java.awt.Color;
//import java.awt.Graphics2D;
//import java.awt.image.BufferedImage;
//import java.io.File;
//import java.io.FileFilter;
//import java.io.IOException;
//import java.util.Vector;
/*import java.util.Collection;
import java.util.Comparator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
//import jvisualfs.ImagePanel;
import java.util.logging.Level;
import java.util.logging.Logger;
*/
import javax.swing.JFileChooser;
/**
 *
 * @author  dud
 */
public class MainUI extends javax.swing.JFrame {
    
    /** Creates new form MainUI */
    public MainUI() {
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        VisPanel = new jvisualfs.ImagePanel();
        jPanel2 = new javax.swing.JPanel();
        txtDirName = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtDirSize = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        btnBack = new javax.swing.JButton();
        btnBrowse = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Vispace"));

        VisPanel.setBackground(new java.awt.Color(255, 255, 255));
        VisPanel.setName("VisPanel"); // NOI18N
        VisPanel.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseMoved(java.awt.event.MouseEvent evt) {
                VisPanelMouseMoved(evt);
            }
        });
        VisPanel.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                VisPanelMouseClicked(evt);
            }
        });

        org.jdesktop.layout.GroupLayout VisPanelLayout = new org.jdesktop.layout.GroupLayout(VisPanel);
        VisPanel.setLayout(VisPanelLayout);
        VisPanelLayout.setHorizontalGroup(
            VisPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 418, Short.MAX_VALUE)
        );
        VisPanelLayout.setVerticalGroup(
            VisPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(0, 440, Short.MAX_VALUE)
        );

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(VisPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(VisPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Information"));

        txtDirName.setFont(new java.awt.Font("Tahoma", 1, 12)); // NOI18N
        txtDirName.setText("DirName");

        jLabel2.setText("Size: ");

        txtDirSize.setText("DirSize");

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(txtDirName, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 186, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jPanel2Layout.createSequentialGroup()
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(txtDirSize)))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(txtDirName)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(txtDirSize))
                .addContainerGap(290, Short.MAX_VALUE))
        );

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder(javax.swing.BorderFactory.createEtchedBorder(), "Navigation"));

        btnBack.setText("Back");
        btnBack.setToolTipText("Click to go up one level");
        btnBack.setEnabled(false);
        btnBack.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBackActionPerformed(evt);
            }
        });

        btnBrowse.setText("Browse...");
        btnBrowse.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnBrowseActionPerformed(evt);
            }
        });

        jLabel1.setText("Please click to select root folder");

        org.jdesktop.layout.GroupLayout jPanel3Layout = new org.jdesktop.layout.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel3Layout.createSequentialGroup()
                .add(jLabel1)
                .addContainerGap())
            .add(jPanel3Layout.createSequentialGroup()
                .add(btnBrowse, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                .addContainerGap())
            .add(jPanel3Layout.createSequentialGroup()
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 196, Short.MAX_VALUE)
                .addContainerGap())
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(126, Short.MAX_VALUE)
                .add(btnBack, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 80, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel3Layout.createSequentialGroup()
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .add(jLabel1)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnBrowse)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(btnBack)
                .add(20, 20, 20))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jPanel3, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 133, Short.MAX_VALUE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void VisPanelMouseMoved(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_VisPanelMouseMoved
// TODO add your handling code here:
        //System.out.println("MOUSEMOVED:" + evt.getX() + ":::" + evt.getY());
        if (VisFX.isCoord(evt.getX(), evt.getY())) {
            //System.out.println("HILIGHT!!!");
            DirStruct tmpStruct = VisFX.getCoordFile(evt.getX(), evt.getY());
            txtVars.setName(tmpStruct.getName());
            //System.out.println("SSIIZZEE!!! " + tmpStruct.getFormattedSize() + "NNAAMMEE!!! " + tmpStruct.getName());
            txtVars.setSize(tmpStruct.getFormattedSize());
            VisFX.setHiLightByCoord(evt.getX(), evt.getY());
        } else {
            txtVars.setName(jvisualfs.MainUI.VisFX.getComp(0).getParent().getName());
            txtVars.setSize(jvisualfs.MainUI.VisFX.getComp(0).getParent().getFormattedSize());
            VisFX.removeAllHiLight();
        }
        VisFX.redraw();        
    }//GEN-LAST:event_VisPanelMouseMoved

    private void VisPanelMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_VisPanelMouseClicked
// TODO add your handling code here:
        if (VisFX.isCoord(evt.getX(), evt.getY())) {
            //System.out.println("HILIGHT!!!");
            DirStruct tmpStruct = VisFX.getCoordFile(evt.getX(), evt.getY());
            
            VisFX.clear();
            VisFX.addAllComps(DirPack, tmpStruct);
            try {
                VisFX.redraw();
            } catch (ArrayIndexOutOfBoundsException e) {
                System.out.println(e);
            }
        }
    }//GEN-LAST:event_VisPanelMouseClicked

private void btnBackActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBackActionPerformed
// TODO add your handling code here:
        DirStruct tmpStruct = VisFX.getComp(0).getParent().getParent();
        //System.out.println("SSIIZZEE!!! " + "NNAAMMEE!!! " + tmpStruct.getName());
        VisFX.clear();
        VisFX.addAllComps(DirPack, tmpStruct);
        VisFX.redraw();
        txtVars.setName(jvisualfs.MainUI.VisFX.getComp(0).getParent().getName());
        txtVars.setSize(jvisualfs.MainUI.VisFX.getComp(0).getParent().getFormattedSize());
}//GEN-LAST:event_btnBackActionPerformed

private void btnBrowseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnBrowseActionPerformed
// TODO add your handling code here:
        JFileChooser fc = new JFileChooser();
        fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int returnVal = fc.showOpenDialog(MainUI.this);
        if( returnVal == JFileChooser.APPROVE_OPTION ) {
            jvisualfs.MainUI.DSRoot = new DirStruct(fc.getSelectedFile());
            //System.out.println("SSIIZZEE!!! " + "NNAAMMEE!!! " + DSRoot.getName());
            DirPack.clear();
            VisFX.clear();
            //VisFX.redraw();
            GenFS.traverse(DirPack.add(DSRoot), DSRoot, DirPack, true);
            DirPack.sort();
            VisFX.addAllComps(DirPack, DSRoot);
            VisFX.redraw();
            txtVars.setName(DSRoot.getName());
            txtVars.setSize(DSRoot.getFormattedSize());
        }
        
}//GEN-LAST:event_btnBrowseActionPerformed
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new MainUI().setVisible(true);
                
            }
        });
        /*Vector FSTest = new Vector();
        FSTest.add(new Vector());
        FSTest[0].add(new DirStruct(new File("/home/dud/jsp-api.jar")));
        FSTest[0].add(new DirStruct(new File("/home/dud/jsp-api.jar")));
        FSTest.add(new DirStruct(new File("/home/dud/jsp-api.jar")));*/
        //GenFX.setImage(VisPanel);
        //DirStruct DSRoot = new DirStruct(new File("c:/dell"));
        //DirStruct DSTest = new DirStruct(new File("/storage/unus/kake/dh/"));
        /*int[] co = { 1, 3 , 5, 6};
        DSTest.setCoord(co);
        System.out.println("L: " + DSTest.getLength() + " N: " + DSTest.getName());
        for (int coord : DSTest.getCoord()) {
            System.out.println("CO: " + coord);
        }*/
        txtVars = new jvisualfs.txtVars();
        //System.out.println("HMM??");
        DirPack = new jvisualfs.DirCollection();
        //GenFS.traverse(DirPack.add(DSRoot), DSRoot, DirPack, true);
        VisFX = new jvisualfs.GenFX(VisPanel);
        //System.out.println("DSPARENT:" + DSTest.getName());
        
        System.out.println("VP:" + VisFX);
        //DirPack.sort();
        //int len = 10;
        /*System.out.println("SIZE " + DirPack.size());
        //if (DirPack.size() < len) {
            //System.out.println("1");
        len = DirPack.size(); 
        //}
        for (int j = 1; j < len; j++) {
            DirStruct tmp = (DirStruct) DirPack.getNum(j);
            //System.out.println("POS: " +  j + " NAME: " + tmp.getName() + " SIZE: " + tmp.getLength() + " PARENT: " + tmp.getParent().getName());
            //if (tmp.hasParent("c:/DeusEx")) {
            if (tmp.getParent().equals(DSTest)) {
                //System.out.println("PARPOS: " +  j + " NAME: " + tmp.getName() + " SIZE: " + tmp.getLength() + " PARENT: " + tmp.getParent().getName());
                VisFX.addComp(tmp);
            }
        }*/
        //VisFX.addAllComps(DirPack, DSRoot);
        //VisFX.redrawBlank();
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public static jvisualfs.ImagePanel VisPanel;
    public static javax.swing.JButton btnBack;
    public static javax.swing.JButton btnBrowse;
    public static javax.swing.JLabel jLabel1;
    public static javax.swing.JLabel jLabel2;
    public static javax.swing.JPanel jPanel1;
    public static javax.swing.JPanel jPanel2;
    public static javax.swing.JPanel jPanel3;
    public static javax.swing.JSeparator jSeparator1;
    public static javax.swing.JLabel txtDirName;
    public static javax.swing.JLabel txtDirSize;
    // End of variables declaration//GEN-END:variables
    public static DirStruct DSRoot;
    public static DirCollection DirPack;
    public static GenFX VisFX;
    public static txtVars txtVars;
}




